CheckSavePath.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. %================================================%
  2. % Prepares and checks for file-save in pipeline: %
  3. % Last modified: Jan.15, 2014 %
  4. %================================================%
  5. %
  6. % NOTE: Currently set to hard-error in pipeline if any problems detected.
  7. % - To continue despite save errors, comment out "error(ErrMsg)" lines.
  8. % Copyright (C) 2013-2014, Michael J. Cheung
  9. %
  10. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  11. % details, see the documentation included with the software package.
  12. %
  13. % MEGPLS is free software: you can redistribute it and/or modify it under
  14. % the terms of the GNU General Public License version 2 as published by
  15. % the Free Software Foundation. This program is distributed in the hope
  16. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  17. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. % See the GNU General Public License for more details.
  19. %
  20. % You should have received a copy of the GNU General Public License along
  21. % with this program. If not, you can download the license here:
  22. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  23. function CheckSavePath(OutputFullpath, ErrLogName)
  24. SaveErrLog = fopen(['ErrorLog_',ErrLogName,'.txt'], 'a');
  25. % Check if directory exists:
  26. [OutFolder, ~, ~] = fileparts(OutputFullpath);
  27. if ~exist(OutFolder, 'dir')
  28. [Status, ErrMsg] = mkdir(OutFolder);
  29. if Status == 0
  30. ErrMsg = sprintf(['ERROR: Failed to create output directory:'...
  31. '\n Reason: %s \n Folder: %s \n\n'], ErrMsg, OutFolder);
  32. fprintf(SaveErrLog, ErrMsg); % Record in ErrorLog
  33. error(ErrMsg);
  34. end
  35. end
  36. % Check write permissions on directory:
  37. [~, DirAttrib] = fileattrib(OutFolder);
  38. if DirAttrib.UserWrite ~= 1
  39. ErrMsg = sprintf(['ERROR: Do not have write permissions for output directory:'...
  40. '\n %s \n\n'], OutFolder);
  41. fprintf(SaveErrLog, ErrMsg); % Record in ErrorLog
  42. error(ErrMsg);
  43. end
  44. % Check for existing file, and remove for overwrite:
  45. if exist(OutputFullpath, 'file')
  46. delete(OutputFullpath);
  47. [Folder, File, Ext] = fileparts(OutputFullpath);
  48. if strcmp(Ext, '.BRIK'); % If .BRIK file, also remove .HEAD
  49. delete([Folder,'/',File,'.HEAD']);
  50. end
  51. if exist(OutputFullpath, 'file')
  52. WarnMsg = sprintf(['WARNING: Could not remove existing file for overwrite.'...
  53. '\n %s \n\n'], OutputFullpath);
  54. fprintf(SaveErrLog, WarnMsg); % Record in ErrorLog
  55. disp(WarnMsg);
  56. end
  57. end
  58. fclose(SaveErrLog);